<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class CookbookController extends AdminbaseController
{

    protected $cookbook_model;
    protected $classify_model;
    protected $clooking_model;

    public function _initialize()
    {
        parent::_initialize();
        $this->cookbook_model = D("Common/Cookbook");
        $this->classify_model = D("Common/Classify");
        $this->cooking_model = D("Common/Cooking");
    }

    // 分页查询后台菜谱列表
    public function index()
    {
        /**搜索条件**/
        $name = I('request.name');
//        $status = I('request.status');
//        if($status == 1){
//            $where = array("is_new"=>1);
//        }
//        if($status == 2){
//            $where = array("is_daka"=>1);
//        }
        $sql = 'SELECT cc.*,cp.name AS cpname,cf.name AS cfname FROM cmf_cookbook cc
                LEFT JOIN cmf_classify cf ON cf.id = cc.classify_pid
                LEFT JOIN cmf_cooking cp ON cp.id = cc.cooking_id 
                WHERE cc.id=cc.id';

        if($name){
            $where['name'] = array('like',"%$name%");
            $sql .= "where cc.name like '%$name%'";
        }
        $count=$this->cookbook_model->where($where)->count();
        $page = $this->page($count, 15);

        $sql .= " ORDER BY cc.listorder  limit $page->firstRow ,$page->listRows";
        $cookbooks = $this->cookbook_model->query($sql);
        $this->assign("page", $page->show('Admin'));
        $this->assign("cookbooks", $cookbooks);
        $this->assign("cookbooks", $cookbooks);
        $this->assign("p", $page->Current_page);
        $this->display();
    }

    // 菜谱添加
    public function add()
    {
        //        查询所有一级分类
        $classify=$this->classify_model->where('pid = 0')->select();
        //        查询所有烹饪方法
        $cookings=$this->cooking_model->select();
                $this->assign("classifys", $classify);
                $this->assign("cookings", $cookings);
        $this->display();
    }

    // 案例添加提交
    public function add_post()
    {
        if (IS_POST) {
            if (empty($_POST['name'])) {
                $this->error("名称不能为空！");
            }
            if (empty($_POST['classify_pid'])) {
                $this->error("请选择案例一级分类！");
            }
//            if (empty($_POST['classify_id'])) {
//                $this->error("请选择菜谱二级分类！");
//            }
//            if (empty($_POST['cooking_id'])) {
//                $this->error("请选择烹饪方法！");
//            }
            if (empty($_POST['image'])) {
                $this->error("案例列表图不能为空！");
            }
            if (empty($_POST['video_image'])) {
                $this->error("案例详情页图标不能为空！");
            }
            if (empty($_POST['ewm_image'])) {
                $this->error("小程序二维码图不能为空！");
            }
            if (empty($_POST['ewm_image'])) {
                $this->error("功能介绍图标不能为空！");
            }
            if (empty($_POST['app_id'])) {
                $this->error("小程序AppID不能为空！");
            }
            if (empty($_POST['description'])) {
                $this->error("案例简介不能为空！");
            }
            if (empty($_POST['content'])) {
                $this->error("案例详情不能为空！");
            }
            $_POST['create_time'] = date("Y-m-d H:i:s", time());
            $_POST['creator_id'] = get_current_admin_id();
            $_POST['content'] = htmlspecialchars_decode($_POST['content']);
            if ($this->cookbook_model->add($_POST) !== false) {

                $this->success(L('ADD_SUCCESS'), U("cookbook/index"));
            } else {
                $this->error(L('ADD_FAILED'));
            }
        }

    }

    // 案例编辑
    public function edit()
    {
        $id = I("get.id", 0, 'intval');
        //        查询所有烹饪方法
        $cookingss=$this->cooking_model->select();

        //        查询所有一级分类
        $classifys=$this->classify_model->where('pid = 0')->select();
        $classify = [];
        $cookbook = $this->cookbook_model->where(array('id' => $id))->find();
        //根据一级分类id查询所有二级分类
        $pid = $cookbook['classify_pid'];
        $sonclassifys=$this->classify_model->where("pid = $pid")->select();
        foreach($cookingss as $co){
            if($co['id'] == $cookbook['cooking_id']){
                $co['cookselected'] = "selected";
            }else{
                $co['cookselected'] = "";
            }
            $cookings[] = $co;
        }

        foreach ($sonclassifys as $so){
            if($so['id'] == $cookbook['classify_id']){
                $so['classselect'] = "selected";
            }else{
                $so['classselect'] = "";
            }
            $sonclassify[] = $so;

        }
        foreach ($classifys as $vo){
            if($vo['id'] == $cookbook['classify_pid']){
                $vo['select'] = "selected";
            }else{
                $vo['select'] = "";
            }
            $classify[] = $vo;

        }
        $this->assign("cookings", $cookings);
        $this->assign("classifys", $classify);
        $this->assign("sonclassifys", $sonclassify);
        $this->assign($cookbook);
        $this->display();
    }

    // 案例编辑提交
    public function edit_post()
    {
        if (empty($_POST['name'])) {
            $this->error("名称不能为空！");
        }
        if (empty($_POST['classify_pid'])) {
            $this->error("请选择案例一级分类！");
        }
//            if (empty($_POST['classify_id'])) {
//                $this->error("请选择菜谱二级分类！");
//            }
//            if (empty($_POST['cooking_id'])) {
//                $this->error("请选择烹饪方法！");
//            }
        if (empty($_POST['image'])) {
            $this->error("案例列表图不能为空！");
        }
        if (empty($_POST['video_image'])) {
            $this->error("案例详情页图标不能为空！");
        }
        if (empty($_POST['ewm_image'])) {
            $this->error("小程序二维码图不能为空！");
        }
        if (empty($_POST['ewm_image'])) {
            $this->error("功能介绍图标不能为空！");
        }
        if (empty($_POST['app_id'])) {
            $this->error("小程序AppID不能为空！");
        }
        if (empty($_POST['description'])) {
            $this->error("案例简介不能为空！");
        }
        if (empty($_POST['content'])) {
            $this->error("案例详情不能为空！");
        }
        $p =  $_GET['p'] ;
        $_POST['update_time'] = date("Y-m-d H:i:s", time());
        $_POST['updator_id'] = get_current_admin_id();
        $_POST['content'] = htmlspecialchars_decode($_POST['content']);
                if ($this->cookbook_model->save($_POST) !== false) {
                    $this->success("保存成功！", U("cookbook/index?p=$p"));
                } else {
                    $this->error("保存失败！");
                }
    }

    // 删除案例
    public function delete()
    {
        $id = I("get.id", 0, "intval");
        if ($this->cookbook_model->delete($id) !== false) {
            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }
    // 推荐/取消推荐
    public function isRecommendToggle()
    {
        if (!empty($_POST['ids']) && isset($_GET["recommend"])) {
            $ids = I('post.ids/a');
            if ($this->cookbook_model->where(array('id' => array('in', $ids)))->save(array('recommended' => 1)) !== false) {
                $this->success("设置推荐成功！");
            } else {
                $this->error("设置推荐失败！");
            }
        }
        if (!empty($_POST['ids']) && isset($_GET["unrecommend"])) {
            $ids = I('post.ids/a');
            if ($this->cookbook_model->where(array('id' => array('in', $ids)))->save(array('recommended' => 0)) !== false) {
                $this->success("取消推荐成功！");
            } else {
                $this->error("取消推荐失败！");
            }
        }
    }


    //通过一级分类获取二级分类

    public function getSonClassify()
    {

        $id = I("get.id", 0, 'intval');
        $classifySon= $this->classify_model->where("pid = $id ")->order('listorder')->select();
        $html = "";
        foreach($classifySon as $k => $v){
            $html .= "<option value='{$v['id']}'>{$v['name']}</option>";
        }
        exit($html);
    }
    //案例排序
    public function listorders() {
        $status = parent::_listorders($this->cookbook_model);
        if ($status) {
            $this->success("排序更新成功！");
        } else {
            $this->error("排序更新失败！");
        }
    }
}